package com.nimt.hqgl_idea.mapper;

import com.nimt.hqgl_idea.entity.Service;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;


@Component(value = "serviceMapper")
public interface ServiceMapper {
    @Insert("insert into service values(null,#{userId},#{typeId},#{content},now(),0,#{userName})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(Service service);

    @Select("<script>" +
            "select a.*,b.userName,c.name as typeName from service a inner join user b on a.userId = b.id inner join service_type c on a.typeId = c.id" +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='userId!=null'> and userId = #{userId}</if>" +
            "<if test='keywords!=null'> and concat(a.content,a.date,b.userName,c.name) like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            " limit #{start},#{pageSize}" +
            "</script>")
    List<Service> selectAll(@Param("start") Integer start,
                            @Param("pageSize") Integer pageSize,
                            @Param("userId") Integer userId,
                            @Param("keywords") String keywords);

    @Select("<script>" +
            "select count(1) from service " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='userId!=null'> and userId = #{userId}</if>" +
            "<if test='keywords!=null'> and content like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            "</script>")
    int count(Integer userId,
              @Param("keywords") String keywords);

    @Select("select * from service where id = #{id}")
    Service selectById(@Param("id") Integer id);

    @Update("<script>" +
            "update service " +
            "<trim prefix='set' suffix='where id=#{id}' suffixOverrides=','>" +
            "<if test='state!=null'> state=#{state},</if>" +
            "<if test='typeId!=null'> typeId=#{typeId},</if>" +
            "<if test='content!=null'> content=#{content},</if>" +
            "</trim>" +
            "</script>")
    int update(Service service);

    @Delete("delete from service where id = #{id}")
    int delete(Integer id);
}
